package main

import "fmt"

//目标Target接口
type Target interface {
	Request()
}

//被适配Adaptee接口
type Adaptee interface {
	SpecificRequest()
}

type XiaoMiAdaptee struct{}

func (xma XiaoMiAdaptee) SpecificRequest() {
	fmt.Println("This is a specific request from Xiao Mi")
}

type HuaWeiAdaptee struct{}

func (hwa HuaWeiAdaptee) SpecificRequest() {
	fmt.Println("This is a specific request from Hua Wei")
}

//适配器Adapter角色,这个结构体里面是一个接口
type Adapter struct {
	Adaptee
}

func (a Adapter) Request() {
	a.SpecificRequest()
}

//使用工厂函数创建Adapter和Adaptee实例
func NewAdapter(adaptee Adaptee) Adapter {
	return Adapter{
		Adaptee: adaptee,
	}
}

func NewAdaptee(t string) Adaptee {
	switch t {
	case "XiaoMi":
		return XiaoMiAdaptee{}
	case "HuaWei":
		return HuaWeiAdaptee{}
	default:
		return nil
	}
}

//测试demo
func main() {
	//创建一个被适配者
	tAdaptee := NewAdaptee("HuaWei")
	//创建一个接受了被适配者接口的结构体,即适配器
	tAdapter := NewAdapter(tAdaptee)
	//调用适配器结构体的Request()方法，这个方法调用接口共有的方法SpecificRequest()
	tAdapter.Request()

	fmt.Println("=============================")

	tAdaptee = NewAdaptee("XiaoMi")
	tAdapter = NewAdapter(tAdaptee)
	tAdapter.Request()
}
